﻿using System;
using System.Collections.Generic;

using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.DAL;
using System.Data;
using System.Text.RegularExpressions;

namespace HZ.Supervise.BLL
{
	public class BLL_Area : IBLL_Area
	{
		/// <summary>
		/// 根据地区编号获取地区信息
		/// </summary>
		/// <param name="AreaID">地区编号</param>
		/// <returns>区域信息</returns>
		public Area GetArea(string AreaID)
		{

			int totalRecord = 0;
			Area area = new Area()
			{
				AreaID = AreaID
			};
			IList<Area> AreaList = DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord);
			if (AreaList.Count > 0)
			{
				area = DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord)[0];
			}
			return area;
		}

		/// <summary>
		/// 获取下一级地区列表
		/// </summary>
		/// <param name="areaid"></param>
		/// <returns></returns>
		public IList<Area> GetSonAreas(string areaid, int currentPage, int pagesize, out int totalRecord)
		{
			return DAL.DAL_Area.GetSonAreas(areaid, currentPage, pagesize, out totalRecord);
		}
		/// <summary>
		/// 根据地区编号获取地区名称
		/// </summary>
		/// <param name="areaid">(多个,隔开)</param>
		/// <returns></returns>
		public string GetAreaNamesByID(string areaid)
		{
			string[] areaids = areaid.Split(',');
			StringBuilder r = new StringBuilder();
			foreach (var item in areaids)
			{
				foreach (var item2 in Areas)
				{
					if (item2.AreaID == item)
					{

						r.AppendFormat(",{0}", item2.AreaName);
					}

				}

			}

			return Regex.Replace(r.ToString(), @"^,", "");
		}
		private static IList<Area> _Areas = null;
		private static IList<Area> Areas
		{
			get
			{
				if (_Areas == null)
				{
					_Areas = GetAreas();
				}
				return _Areas;
			}
		}

		private static IList<Area> GetAreas()
		{
			string areaid = System.Configuration.ConfigurationManager.AppSettings["CURRENT_DEPLOY_AREAID"];

			BLL_Area ba = new BLL_Area();
			return DAL_Helper.CommonFillList<Area>(HZ.Data.DbHelperFactory.Create().ExecuteTable(string.Format("SELECT * FROM TB_Area WHERE FatherAreaID = '{0}' OR AreaID = '{0}'", areaid)));
		}

		#region IBLL_Area 成员


		public IList<Area> GetAreasByAreaID(string Areaid)
		{
			return DAL.DAL_Area.GetAreasByAreaID(Areaid);
		}

		#endregion

		/// <summary>
		/// 获取下一级地区列表
		/// </summary>
		/// <param name="areaid"></param>
		/// <returns></returns>
		public IList<Area> GetSonAreas(string areaid)
		{
			return DAL.DAL_Area.GetSonAreas(areaid);

		}
		/// <summary>
		/// 获取区域之星
		/// </summary>
		/// <returns></returns>
		public DataSet GetProcureAreaMax(string newsqlwhere, string oldsqlwhere, string orderwhere, string sqlArea)
		{
			return DAL.DAL_Area.GetProcureAreaMax(newsqlwhere, oldsqlwhere, orderwhere, sqlArea);
		}

		/// <summary>
		/// 获取生成树形菜单所需的数据
		/// 董辉
		/// 2011-4-24 23:38
		/// </summary>
		/// <returns></returns>
		public string GetJSON(string areaid)
		{
			return new DAL.DAL_Area().GetJSON(areaid);
		}
		/// <summary>
		/// 曹鹏
		/// 2011-4-27 8:55:44
		/// 根据地区县的名称得到该地区的去名称（某某市某某县）
		/// </summary>
		public string GetCityNameByCountryName(string contryName)
		{

			return new DAL.DAL_Area().GetCityNameByCountryName(contryName);

		}

		/// <summary>
		/// 曹鹏
		/// 2011年5月17日9:13:56
		/// 地区排名（县区）
		/// </summary>
		/// <param name="orderOfRank"></param>
		/// <param name="orderBy"></param>
		/// <param name="wherePm"></param>
		/// <param name="wherePms"></param>
		/// <param name="currentpage"></param>
		/// <param name="pagesize"></param>
		/// <param name="totalrecord"></param>
		/// <returns></returns>
		public DataTable GetProcureAreaMax1(string orderOfRank, string orderBy, string wherePm, string wherePms,string sortSource, int currentpage, int pagesize, out int totalrecord)
		{
			return HZ.Supervise.DAL.DAL_Area.GetProcureAreaMax1(orderOfRank, orderBy, wherePm, wherePms,sortSource, currentpage, pagesize, out  totalrecord);
		}
		/// <summary>
		/// 曹鹏
		/// 2011年5月17日9:13:56
		/// 地区排名（市）
		/// </summary>
		/// <param name="orderOfRank"></param>
		/// <param name="orderBy"></param>
		/// <param name="wherePm"></param>
		/// <param name="wherePms"></param>
		/// <param name="currentpage"></param>
		/// <param name="pagesize"></param>
		/// <param name="totalrecord"></param>
		/// <returns></returns>
		public DataTable GetProcureAreaMax2(string strgroupBy,string orderOfRank, string orderBy, string wherePm, string wherePms, string sortSource, int currentpage, int pagesize, out int totalrecord)
		{
			return HZ.Supervise.DAL.DAL_Area.GetProcureAreaMax2(strgroupBy,orderOfRank, orderBy, wherePm, wherePms, sortSource, currentpage, pagesize, out  totalrecord);
		}

		#region IBLL_Area 成员

		/// <summary>
		/// 曹鹏
		/// 2011年5月24日17:37:34
		/// 取得该地区下面的子地区，如果该地区没有子地区，那么就显示它本身
		/// </summary>
		/// <param name="flag"></param>
		/// <param name="areaid"></param>
		/// <param name="currentPage"></param>
		/// <param name="pagesize"></param>
		/// <param name="totalRecord"></param>
		/// <returns></returns>
		public IList<Area> GetAreas(bool flag, string areaid, int currentPage, int pagesize, out int totalRecord)
		{
			return HZ.Supervise.DAL.DAL_Area.GetAreas(flag, areaid, currentPage, pagesize,out totalRecord);
		}

		#endregion
	}
}
